var NotificationCenter = require('../tools/SimpleNotificationCenter');


cc.Class({
    extends: cc.Component,

    properties: {
        //名称
        itemName: "未知种子",
        //对应的图像地址
        textureUrl: "textures/icons.plist/seed_0",
        //单价
        price: 0,
        //触摸次数 用来计算双击操作的
        touchCount: 0,

        //模型
        model: {
            default: null,
            type: Object
        }
    },

    // use this for initialization
    onLoad: function () {
        this.layoutUI()
        this.setEvent();
    },
    setEvent: function () {
        //为当前节点设置值
        this.node.on('touchend', function (event) {
            //阻止冒泡
            event.stopPropagation();

            if (this.touchCount > 0) {

                //通知关闭植物Store
                NotificationCenter.notify("hideSeedStore", this);
                //通知土地更新植物
                NotificationCenter.notify("pickedSeed", this, {
                    isClose: false,
                    model: this.model
                });
            } else {
                this.touchCount += 1;
                //设置定时器归零
                this.scheduleOnce(function () {
                    this.touchCount = 0;
                    cc.log("Clear");
                }.bind(this), 1);
            }


        }, this);
    },

    //进行UI布局
    layoutUI: function () {
        cc.loader.loadRes(this.textureUrl, function (err, spriteFrame) {

            if (err) {
                //cc.log(err);
                return;
            }


            this.node.getChildByName('Icon').getComponent(cc.Sprite).spriteFrame = spriteFrame;
            //设置文本
            this.node.getChildByName('Name').getComponent(cc.Label).string = this.itemName;
            this.node.getChildByName('Price').getComponent(cc.Label).string = "价格:" + this.price
        }.bind(this));
    }
});
